package com.hdaccp.shuiguoD.controller;

import com.hdaccp.shuiguoD.dao.RolePermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/rolepermission")
public class RolePermissionController {
    @Autowired
    private RolePermissionDao rolePermissionDao;
    @RequestMapping("/grantPermission")
    @ResponseBody
    public Map<String,Object> grantPermission(int rid,String pmid){
        Map<String,Object> map = new HashMap<>();
        //System.out.println(rid+">>"+pmid);

        try {
        String [] ss = pmid.split("-");
        long [] perids = new long[ss.length - 1];
        for (int i=0;i<perids.length;i++){
            perids[i] = Integer.parseInt(ss[i+1]) ;
        }

        //删除角色对应的权限
        rolePermissionDao.del(rid);

        //增加用户选择的权限
        for (long pid : perids){
            rolePermissionDao.save(rid,pid);
        }
        map.put("msg","1") ;
    } catch (NumberFormatException e) {
        map.put("msg","2") ;
        e.printStackTrace();
    }
        return map;
    }





}
